package com.spring.oaassist.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.oaassist.web.model.CourseClass;
import com.spring.oaassist.web.model.CourseClassBaby;
import com.spring.oaassist.web.service.UserClassManageService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.model.BaseResult;
import com.sso.api.web.dao.SsoMapper;
import com.sso.api.web.model.Baby;

@Controller
@RequestMapping(value = "/userclass")
public class UserClassManageController extends BaseController{
	
	@Resource UserClassManageService userClassManageService;
	
	@Resource SsoMapper ssoMapper;
	
//	@OaUserPermission(value="/userclass/index")
	@RequestMapping(value = "/index")
    public ModelAndView index(String course_id,Model model,HttpServletRequest request){
		model.addAttribute("course_id", course_id);
		return new ModelAndView("main/userclass/index");
    }
	
	/**
	 * 课程下拉
	 * @param type
	 * @param request
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "/getCourseClass")
	@ResponseBody
	public Object getCourseClass(HttpServletRequest request,
			String callback) {
		List<CourseClass> m = userClassManageService.getCourseClass(null);
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/classUserList")
	@ResponseBody
	public Object classUserList(String page,String rows,
			String class_id,String babyName,HttpServletRequest request){
		Map<String, Object> retD = new HashMap<String, Object>();
		if(!ApplicationUtils.isNotNull(class_id)&&!ApplicationUtils.isNotNull(babyName)){
			retD.put("total", 0);
	        retD.put("rows", new ArrayList<Map<String,Object>>()); 
			return retD;
		}
	    Map<String, Object> parm = new HashMap<String, Object>();
	    
	    parm.put("class_id", class_id);
	    parm.put("babyName", babyName);
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<Map<String,Object>> list = userClassManageService.classUserList(parm);
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/userList")
	@ResponseBody
	public Object userList(String page,String rows,String real_name,String phone,
			HttpServletRequest request){
		Map<String, Object> retD = new HashMap<String, Object>();
	    Map<String, Object> parm = new HashMap<String, Object>();
	    if(!ApplicationUtils.isNotNull(real_name)&&!ApplicationUtils.isNotNull(phone)){
	    	retD.put("total", 0);
	        retD.put("rows", new ArrayList<Baby>()); 
	    	return retD;
	    }
	    if(ApplicationUtils.isNotNull(real_name)){
	    	parm.put("real_name", real_name);
	    }
	    if(ApplicationUtils.isNotNull(phone)){
	    	parm.put("phone", phone);
	    }
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
    	List<Baby> list = ssoMapper.getUserBabyList(parm);
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	/**
	 * 删除课程学生
	 * @param ids
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteCourseClassUser")
	@ResponseBody
	public Object deleteCourseClassUser(String ids, HttpServletRequest request) {
		ShopOaSessionUtil.setOaSession(request);
		BaseResult ret = userClassManageService.deleteCourseClassUser(ids,request);
		return buildResultInfo(ret);
	}
	
	
	/**
	 * 添加课程学生
	 * @param ids
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/addCourseClassUser")
	@ResponseBody
	public Object addCourseClassUser(@RequestBody List<CourseClassBaby> data,HttpServletRequest request) throws Exception {
		ShopOaSessionUtil.setOaSession(request);
		BaseResult ret = userClassManageService.addCourseClassUser(data,request);
		return buildResultInfo(ret);
	}
}
